package com.ruoyi.business.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * EasyPoi 工具类
 *
 * @author Tellsea
 * @date 2022/12/3
 */
@Slf4j
public class EasyPoiExcelUtils {

    public static void exportExcel(List<?> list, Class<?> pojoClass) {
        ExportParams exportParams = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel("导入模板-" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN) + ".xlsx", workbook);
    }

    public static void defaultExport(List<?> list, Class<?> pojoClass, String title, String fileName) {
        ExportParams exportParams = new ExportParams(title, "Sheet1");
        exportParams.setAutoSize(true);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel(fileName, workbook);
    }

    public static void downLoadExcel(String fileName, Workbook workbook) {
        try {
            HttpServletResponse response = ServletUtils.getResponse();
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("下载失败");
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        return defaultImportExcel(file, pojoClass, params);
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass, int startSheetIndex) {
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        params.setStartSheetIndex(startSheetIndex);
        return defaultImportExcel(file, pojoClass, params);
    }

    public static <T> List<T> defaultImportExcel(MultipartFile file, Class<T> pojoClass, ImportParams params) {
        if (Objects.isNull(file)) {
            throw new ServiceException("找不到文件");
        }
        List<T> list;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            e.printStackTrace();
            throw new ServiceException("文件不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("导入失败");
        }
        return list;
    }

    public static Map<String, Object> createSheet(String sheetName, Class<?> pojoClass, List<?> list) {
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName(sheetName);
        Map<String, Object> sheetMap = new HashMap<>(16);
        sheetMap.put("title", exportParams);
        sheetMap.put("entity", pojoClass);
        sheetMap.put("data", list);
        return sheetMap;
    }

    public static void exportExcel(List<Map<String, Object>> sheetList, String title) {
        Workbook workbook = ExcelExportUtil.exportExcel(sheetList, ExcelType.HSSF);
        downLoadExcel(title, workbook);
    }
}
